package com.jcl.recruit.user.mapper;

import com.jcl.recruit.user.model.UserExternalAccess;
import org.springframework.util.StringUtils;

import java.util.Date;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class UserExternalAccessProvider {

    public String insertProvider(UserExternalAccess userExternalAccess){
        BEGIN();
        INSERT_INTO("user_external_access");
        if(null != userExternalAccess.getUserId()){
            VALUES("user_id", "#{userId,jdbcType=INTEGER}");
        }

        if(StringUtils.hasLength(userExternalAccess.getCorpCode())){
            VALUES("corp_code", "#{corpCode,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getExternalKey()){
            VALUES("external_key", "#{externalKey,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getExternalUnionKey()){
            VALUES("external_union_key", "#{externalUnionKey,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getNickname()){
            VALUES("nickname", "#{nickname,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getUsername()){
            VALUES("username", "#{username,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getPassword()){
            VALUES("password", "#{password,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getStatus()){
            VALUES("status", "#{status,jdbcType=INTEGER}");
        }
        if(null != userExternalAccess.getCategory()){
            VALUES("category", "#{category,jdbcType=INTEGER}");
        }
        if(null != userExternalAccess.getCreateTime()){
            VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }
        if(null != userExternalAccess.getModifyTime()){
            VALUES("modify_time", "#{modifyTime,jdbcType=TIMESTAMP}");
        }
        return SQL();
    }

    public String updateProvider(UserExternalAccess userExternalAccess){
        BEGIN();
        UPDATE("user_external_access");
        if(null != userExternalAccess.getUserId()){
            SET("user_id=#{userId,jdbcType=INTEGER}");
        }
        if(StringUtils.hasLength(userExternalAccess.getCorpCode())){
            SET("corp_code=#{corpCode,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getExternalKey()){
            SET("external_key=#{externalKey,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getExternalUnionKey()){
            SET("external_union_key=#{externalUnionKey,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getNickname()){
            SET("nickname=#{nickname,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getUsername()){
            SET("username=#{username,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getPassword()){
            SET("password=#{password,jdbcType=VARCHAR}");
        }
        if(null != userExternalAccess.getStatus()){
            SET("status=#{status,jdbcType=INTEGER}");
        }
        if(null != userExternalAccess.getCategory()){
            SET("category=#{category,jdbcType=INTEGER}");
        }
        userExternalAccess.setModifyTime(new Date());
        if(null != userExternalAccess.getModifyTime()){
            SET("modify_time=#{modifyTime,jdbcType=TIMESTAMP}");
        }

        WHERE("id = #{id,jdbcType=INTEGER}");
        return SQL();
    }

}
